package com.smart.common.jna;

import com.sun.jna.*;

import java.util.Arrays;
import java.util.List;

public interface OTAPCMS extends Library {

    OTAPCMS INSTANCE = (OTAPCMS) Native.loadLibrary(System.getProperty("user.dir") + "\\HCOTAPCMS.dll", OTAPCMS.class);
    //OTAPCMS INSTANCE = (OTAPCMS) Native.loadLibrary(System.getProperty("user.dir") + "\\libHCOTAPCMS.so", OTAPCMS.class);

    // OTAP_IPADDRESS
    public static class OTAP_IPADDRESS extends Structure {
        public byte[] szIP = new byte[128]; //IP
        public short wPort;                 //Port
        public byte[] byRes = new byte[6];  //Reserved

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("szIP", "wPort", "byRes");
        }
    }

    // OTAP_CMS_RegisterCallback
    public static interface OTAP_CMS_RegisterCallback extends Callback {
        public boolean invoke(int iUserID, int dwDataType, Pointer pOutBuffer, int dwOutLen, Pointer pInBuffer, int dwInLen, Pointer pUserData);
    }

    public static interface OTAP_CMS_InterfaceAsyncCallback extends Callback {
        public boolean invoke(int iUserID, int dwDataType, Pointer pOutBuffer, int dwOutLen, Pointer pInBuffer, int dwInLen, Pointer pUserData);
    }

    // OTAP_CMS_LISTEN_PARAM
    public static class OTAP_CMS_LISTEN_PARAM extends Structure {
        public OTAP_IPADDRESS struAddress;
        public OTAP_CMS_RegisterCallback fnCB;
        public Pointer pUserData;
        public byte[] byRes = new byte[128];

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("struAddress", "fnCB", "pUserData", "byRes");
        }
    }

    // OTAP_CMS_DEV_REG_INFO
    public static class OTAP_CMS_DEV_REG_INFO extends Structure {
        public OTAP_IPADDRESS struDevAddr = new OTAP_IPADDRESS();
        public byte[] byDeviceID = new byte[256];
        public byte[] byDeviceName = new byte[128];
        public byte[] byDeviceSerial = new byte[256];
        public byte[] byDeviceFullSerial = new byte[256];
        public byte[] byFirmwareVersion = new byte[64];
        public int dwDevType;
        public int dwManufacture;
        public byte[] bySessionKey = new byte[64];
        public byte[] byIV = new byte[32];
        public OTAP_IPADDRESS struRegAddr = new OTAP_IPADDRESS();
        public byte byCompress;
        public int dwDevTypeLen;
        public int dwDevTypeDisplayLen;
        public Pointer pDevType;
        public Pointer pDevTypeDisplay;
        public byte[] byRes = new byte[256];

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("struDevAddr", "byDeviceID", "byDeviceName", "byDeviceSerial", "byDeviceFullSerial",
                    "byFirmwareVersion", "dwDevType", "dwManufacture", "bySessionKey", "byIV", "struRegAddr",
                    "byCompress", "dwDevTypeLen", "dwDevTypeDisplayLen", "pDevType", "pDevTypeDisplay", "byRes");
        }
    }

    // OTAP_DEV_SESSIONKEY
    public static class OTAP_DEV_SESSIONKEY extends Structure {
        public byte[] byDeviceID = new byte[256];
        public byte[] bySessionKey = new byte[64];
        public byte[] byIV = new byte[32];
        public byte[] byRes = new byte[128];

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("byDeviceID", "bySessionKey", "byIV", "byRes");
        }
    }

    // OTAP_CMS_DAS_INFO
    public static class OTAP_CMS_DAS_INFO extends Structure {
        public OTAP_IPADDRESS struDevAddr = new OTAP_IPADDRESS();
        public byte[] byDomain = new byte[64];
        public byte[] byServerID = new byte[64];
        public byte[] byRes = new byte[248];

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("struDevAddr", "byDomain", "byServerID", "byRes");
        }
    }

    // OTAP_CMS_CONFIG_DEV_PARAM
    public static class OTAP_CMS_CONFIG_DEV_PARAM extends Structure {
        public byte[] szChildID = new byte[32];
        public byte[] szLocalIndex = new byte[32];
        public byte[] szResourceType = new byte[64];
        public byte[] szDomain = new byte[64];
        public byte[] szIdentifier = new byte[64];
        public Pointer pInBuf;
        public Pointer pOutBuf;
        public int dwInBufSize;
        public int dwOutBufSize;
        public int iAsyncHandle;
        public byte[] byRes = new byte[252];

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("szChildID", "szLocalIndex", "szResourceType", "szDomain", "szIdentifier", "pInBuf",
                    "pOutBuf", "dwInBufSize", "dwOutBufSize", "iAsyncHandle", "byRes");
        }
    }
    public static class OTAP_CMS_ISAPI_PT_PARAM extends Structure {
        public Pointer pRequestUrl;
        public int dwRequestUrlLen;
        public Pointer pCondBuffer;
        public  int dwCondSize;
        public  Pointer pInBuffer;
        public  int dwInSize;
        public  Pointer pOutBuffer;
        public  int dwOutSize;
        public  int dwReturnedLen;
        public  int dwRecvTimeOut;
        public  long iHandle;
        public  Pointer pUserData;
        public  byte[] byRes = new byte[16];
        public  OTAP_CMS_InterfaceAsyncCallback fnCB;

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("pRequestUrl", "dwRequestUrlLen", "pCondBuffer", "dwCondSize", "pInBuffer", "dwInSize", "pOutBuffer", "dwOutSize", "dwReturnedLen", "dwRecvTimeOut", "iHandle", "pUserData", "byRes", "fnCB");
        }
    }

    boolean OTAP_CMS_Init();
    boolean OTAP_CMS_Fini();
    int OTAP_CMS_GetLastError();
    boolean OTAP_CMS_SetLogToFile(int iLogLevel, String strLogDir, boolean bAutoDel);
    int OTAP_CMS_StartListen(OTAP_CMS_LISTEN_PARAM lpCMSListenPara);
    boolean OTAP_CMS_StopListen(int iHandle);
    boolean OTAP_CMS_ConfigDev(int iUserID, int enumMsg, OTAP_CMS_CONFIG_DEV_PARAM pParam);
    boolean OTAP_CMS_ISAPIPassThrough(int iUserId, OTAP_CMS_ISAPI_PT_PARAM  pParam);
    boolean OTAP_CMS_ResponseMsg(Long iUserID,  String enumMsg, OTAP_CMS_CONFIG_DEV_PARAM pParam);


}
